package frame.laxcen.test;

import frame.laxcen.collection.ConstPair;
import frame.laxcen.request.Request_0010_10;
import frame.laxcen.module.RFIDReader;
import tcpclient.v2.RFIDClient;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Optional;
import java.util.Set;

public class RFIDReader_Recycle {

    public static void sequenceRead(RFIDReader reader) {

        if(reader.tryConnect(new InetSocketAddress("192.168.1.116", 9090), 3000)) {

            System.out.println("连接成功！");

            Request_0010_10.RequestTemplate template = new Request_0010_10.RequestTemplate();

            template.useAntenna_1();

            template.standerSequenceRead();

            if(reader.beginRead(new Request_0010_10(template), 3000)) System.out.println("成功启动读！");

            for(int i = 0; i < 5; ++i) {

                System.out.println("第" + i + "次获取数据！");

                long begin = System.currentTimeMillis();

                reader.collectResponseSet(1000).forEach((pair) -> System.out.println("Tid: [" + pair.getValue1() + "] UserData: [" + pair.getValue2() + "]"));

                System.out.println("耗时:" + (System.currentTimeMillis() - begin) + " ms");
            }

            if(reader.endRead(3000)) System.out.println("停止成功！");

            if(reader.tryClose()) System.out.println("关闭连接！");
        }
    }

    public static void singleRead(RFIDReader reader) {

        if(reader.tryConnect(new InetSocketAddress("192.168.1.116", 9090), 3000)) {

            System.out.println("连接成功！");

            Request_0010_10.RequestTemplate template = new Request_0010_10.RequestTemplate();

            template.useAntenna_1();

            template.standerSingleRead();
            Optional<ConstPair<String, String>> resultSet = reader.pointRead(new Request_0010_10(template),3000);

            System.out.println("进行单次读！");

            resultSet.ifPresent((e) -> System.out.println("Tid: [" + e.getValue1() + "] UserData: [" + e.getValue2() + "]"));

            if(reader.tryClose()) System.out.println("关闭连接！");
        }
    }

    public static void main(String[] args) throws IOException {

        // 将 FrameSize 设置为较小值才会触发回收操作

        RFIDReader reader = new RFIDReader(new RFIDClient(100, 1024));

        // 测试单次读或连续读

        sequenceRead(reader);

//        singleRead(reader);
    }
}
